<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: LanguageController.php 259 2011-07-22 08:44:00Z beimuaihui $
 */



class Qa_QareportController extends Baogg_Controller_Action {

	function init() {


	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;

		$params=$this->getRequest()->getParams();
		$form_url=$params;

		Zend_Loader::loadClass("Baogg_View_Extgrid");

		$cm=array();
		$cm['qa_report_id']=array("cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['qa_report_name']=array("sortable"=>true);
		
		//'tpl'=>'{qa_report_category_name}','xtype'=>'templatecolumn'
		$cm['qa_report_category_id']=array("hidden"=>true);
		$cm['qa_report_category_id2']=array('is_group'=>true);
		
		$cm['qa_surveys_id']=array("hidden"=>false,'tpl'=>'{qa_surveys_name}','xtype'=>'templatecolumn');		
		$cm['qa_surveys_name']=array("hidden"=>true,"sortable"=>false);
		$cm['ordering']=array("sortable"=>true);
		$cm['state']=array("hidden"=>true);
		$cm['state2']=array("sortable"=>false);
		//$cm['action']=array("sortable"=>false);
		//$cm['ordering']=array("sortable"=>true);
		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;

		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		//$grid->setRowNumberer(true);
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete'));
		//$grid->registerButton("fields","window.location.href='".BAOGG_BASE_URL."dict/levelfield/list/';");

		$this->view->form_url=BAOGG_BASE_URL."dict/level/save";
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();

		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>300,"width"=>600));

		$QaSurveys=new QaSurveys();
		$store_survey=$QaSurveys->getCombo(array('state'=>1),array('sort'=>'ordering','dir'=>'asc'),null,array('qa_surveys_id','qa_surveys_name'));
		
		$QaReportCategory=new QaReportCategory();
		$store_category=$QaReportCategory->getCombo(array('state'=>1),null,null,array('qa_report_category_id','qa_report_category_name'));
		//echo '<pre>';print_r($store_survey);exit;

		$form->addItem(0,array('name'=>'qa_report_name'));
		$form->addItem(0,array('name'=>'qa_report_category_id','xtype'=>'combo','store'=>$store_category));
		$form->addItem(0,array('name'=>'qa_surveys_id','xtype'=>'combo','store'=>$store_survey));
		$form->addItem(1,array('name'=>'ordering'));
		$form->addItem(2,array('name'=>'state','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(2,array('name'=>'qa_report_id',"xtype"=>"hidden","hideLabel"=>true));
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));//,'config','config'

		//$form->addExistButtons(array('search','add','edit','delete','remove','reset'),array('edit','delete'));


		$this->view->form=$form;

		//echo '<pre>';var_dump($LANG);exit;


	}

	/**
	 * The data action
	 */
	function dataAction() {
		global $LANG;

		$QaReport=new QaReport();
		$post=$this->_request->getParams ();
		$where=array();$order=array();$limit=array();
		foreach((array)$post as $k=>$v){
			if($k=="start" || $k=="limit"){
				$limit[$k]=$v;
			}else if($k=="sort" || $k=="dir"){
				$order[$k]=$v;
			}else{
				if($k=="qa_report_id"  ){
					if($v && is_numeric($v)){
						$v=(int)$v;
						$where[$k]=$v;
					}
				}else{
					$v='%'.$v.'%';
					$where[$k]=$v;
				}

			}
		}

		$rs=$QaReport->getList($where,$order,$limit);

		//get survey name
		$QaSurveys=new QaSurveys();
		$arr_survey_name=array();
		
		$QaReportCategory=new QaReportCategory();
		$arr_category=array();
		
		$arr_state=Baogg_Language::get('arr_state');
		foreach((array)$rs as $k=>$v){
			
			if(!isset($arr_survey_name[$v['qa_surveys_id']])){
				$rs_surveys=$QaSurveys->getOne($v['qa_surveys_id']);				
				$arr_survey_name[$v['qa_surveys_id']]=@$rs_surveys[0]['qa_surveys_name'];
			}
			$rs[$k]['qa_surveys_name']=$arr_survey_name[$v['qa_surveys_id']];
			
			if(!isset($arr_category[$v['qa_report_category_id']])){
				$rs_category=$QaReportCategory->getOne($v['qa_report_category_id']);				
				$arr_category[$v['qa_report_category_id']]=@$rs_category[0]['qa_report_category_name'];
			}		
			$rs[$k]['qa_report_category_id2']=$arr_category[$v['qa_report_category_id']];
			
			
			$rs[$k]['state2']=$arr_state[$v['state']];
		}

		$rs_cnt=$QaReport->getList($where,NULL,NULL,"count(*) as cnt");
		echo  Zend_Json::encode(array("count"=>$rs_cnt[0]['cnt'],"data"=>$rs));
		Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
	}


	/**
	 * The add action
	 */
	function addAction(){
		$this->saveAction();
	}

	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}


	/**
	 * The delete action
	 */
	function deleteAction() {

		$ret=array("success"=>false,"msg"=>"delete_fail");
		$id = Baogg_Controller_Action::_getParam ( 'ids' );
			
		 
		$QaReport=new QaReport();
		
		if ($QaReport->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}

		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();
	}

	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;

		$ret=array("success"=>false,"msg"=>"save_fail");
	  
		$QaReport=new QaReport();
		//check form
		if (!$this->_request->isPost ())
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}

		$formData = $this->_request->getPost ();

		if(!$formData)
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}
			
		$arr=$QaReport->filterForm($formData);
		$action=$this->getRequest()->getParam('action');
		$rs = $QaReport->getSame(array('qa_report_name'=>$arr['qa_report_name']));

		if($action=='edit'){
			if(count($rs)==0 || count($rs)==1 &&  $rs[0] == $arr["qa_report_id"] ){
				$QaReport->_update($arr,$arr["qa_report_id"]);
				$ret['success'] = true;
				$ret['msg'] = "save_ok";
			}else{
				$ret['msg'] = "exist_same_record";
			}
				
		}else{
			//insert record
			//$permission_type = 1;
			if($rs){
				$ret['msg'] = "exist_same_record";
			}else{
				unset($arr["qa_report_id"]);
				$qa_report_id=$QaReport->_insert($arr);
				$ret['success'] = $qa_report_id>0?true:false;
				$ret['msg'] = "add_ok";

				$Permission=new Permission();
				$Permission->insertUserReport($qa_report_id);
			}
		}

		echo Baogg_Language::outputResult($ret);

		$this->_helper->viewRenderer->setNoRender ();


	}

}
